﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

/*
 * Opisuje prymityw przedstawiający kwadrat.
 */
namespace Projekt.Models.Primitives
{
    public class Square : DrawableGameComponent
    {
        Engine game;

        VertexPositionColor[] vertices;
        private GraphicsDevice graphicsDevice;
        BasicEffect basicEffect;
        Matrix Transforms;

        private Vector3 position;
        public Vector3 Position
        {
            get { return position; }
            set
            {
                position = value;
                Transform();
            }
        }
        private Vector3 rotation;
        public Vector3 Rotation
        {
            get { return rotation; }
            set
            {
                rotation = value;
                Transform();
            }
        }
        private Vector3 scale;
        public Vector3 Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                Transform();
            }
        }

        /// <summary>
        /// Tworzy kwadrat o podanych parametrach
        /// </summary>
        /// <param name="Position">Punkt centralny</param>
        /// <param name="Rotation">Wektor obrotu</param>
        /// <param name="Scale">Wymiary</param>
        /// <param name="graphicsDevice"></param>
        public Square(Vector3 Position, Vector3 Rotation, Vector3 Scale, Color color, GraphicsDevice graphicsDevice, Engine game)
            : base(game)
        {
            this.graphicsDevice = graphicsDevice;
            this.Position = Position;
            this.Rotation = Rotation;
            this.Scale = Scale;
            this.game = game;
            this.game.Components.Add(this);

            vertices = new VertexPositionColor[5];

            //front
            vertices[0].Position = new Vector3(-0.5f, 0.5f, 0.5f);
            vertices[0].Color = color;
            vertices[1].Position = new Vector3(0.5f, 0.5f, 0.5f);
            vertices[1].Color = color;
            vertices[2].Position = new Vector3(0.5f, -0.5f, 0.5f);
            vertices[2].Color = color;
            vertices[3].Position = new Vector3(-0.5f, -0.5f, 0.5f);
            vertices[3].Color = color;
            vertices[4].Position = new Vector3(-0.5f, 0.5f, 0.5f);
            vertices[4].Color = color;

            basicEffect = new BasicEffect(graphicsDevice);
            basicEffect.VertexColorEnabled = true;
        }

        /// <summary>
        /// Oblicza macierz transformacji.
        /// </summary>
        public void Transform()
        {
            Transforms = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position);
        }

        public override void Draw(GameTime gameTime)
        {
            basicEffect.Projection = game.camera.Projection;
            basicEffect.View = game.camera.View;
            basicEffect.World = Transforms;

            basicEffect.CurrentTechnique.Passes[0].Apply();

            graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, 0, 4);
            
            base.Draw(gameTime);
        }
    }
}
